package cn.itcast.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.itcast.entity.Result;
import cn.itcast.service.ExcelExportService;
import cn.itcast.service.ReportService;
import cn.itcast.vo.*;
import com.alibaba.excel.EasyExcel;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import java.net.URLEncoder;


import java.util.List;

@RestController
@RequestMapping("/report")
@CrossOrigin
public class ReportController {
    @Autowired
    private ReportService reportService;
    @Autowired
    private ExcelExportService excelExportService;
    /**
     * 会员数量统计
     * @return
     */
    @GetMapping("member/count")
    public Result reportMember(){
        Result result=new Result();
        MemberReportVo memberReportVo = reportService.selectCountGroupByMonth();
        if (ObjectUtil.isEmpty(memberReportVo)){
            result.setFlag(false);
            result.setMessage("年会员统计错误!");
            return result;
        }
        result.setMessage("年会员统计成功");
        result.setFlag(true);
        result.setData(memberReportVo);
        return result;
    }
    /**
     * 套餐预约统计
     * @return
     */
    @GetMapping("setmeals")
    public Result reportSetMeal(){
        Result result=new Result();
        SetMealReportVo setMealReportVo = reportService.selectSetMealCount();
        if (ObjectUtil.isEmpty(setMealReportVo)){
            result.setFlag(false);
            result.setMessage("套餐统计错误!");
            return result;
        }
        result.setMessage("套餐统计成功");
        result.setFlag(true);
        result.setData(setMealReportVo);
        return result;
    }
    /**
     * 每月收入统计
     * @return
     */
    @GetMapping("income")
    public Result reportIncome(){
        Result result=new Result();
        IncomeReportVo incomeReportVo = reportService.selectIncomeGroupByMonth();
        System.out.println(incomeReportVo);
        if (ObjectUtil.isEmpty(incomeReportVo)){
            result.setFlag(false);
            result.setMessage("年收入统计错误!");
            return result;
        }
        result.setMessage("年收入统计成功");
        result.setFlag(true);
        result.setData(incomeReportVo);
        return result;
    }
    /**
     * 预约/支付统计
     * @return
     */
    @GetMapping("order")
    public Result reportOrder(){
        Result result=new Result();
        OrderReportVo orderReportVo = reportService.selectOrderAndPayCount();
        /*System.out.println(orderReportVo);*/
        if (ObjectUtil.isEmpty(orderReportVo)){
            result.setFlag(false);
            result.setMessage("预定和支付统计错误!");
            return result;
        }
        result.setMessage("预定和支付统计成功");
        result.setFlag(true);
        result.setData(orderReportVo);
        return result;
    }
    /**
     * 运营数据统计
     * @return
     */
    @GetMapping("business")
    public Result reportBusiness(){
        Result result=new Result();
        BusinessReportVo businessReportVo = reportService.selectBusiness();
        /*System.out.println(orderReportVo);*/
        if (ObjectUtil.isEmpty(businessReportVo)){
            result.setFlag(false);
            result.setMessage("运营数据统计错误!");
            return result;
        }
        result.setMessage("运营数据统计成功");
        result.setFlag(true);
        result.setData(businessReportVo);
        return result;
    }/**
     * 运营数据excel导出
     * @return
     */
    @GetMapping("export/business")
    public void exportExcel(HttpServletResponse response) throws IOException {
        List<BusinessExcel> businessExcels = excelExportService.exportExcel();
        if(ObjectUtil.isNotEmpty(businessExcels)){
            String fileName = URLEncoder.encode("business", "UTF-8");
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName+".xlsx" );
            EasyExcel.write(response.getOutputStream(), BusinessExcel.class)
                    .sheet("sheet")
                    .doWrite(businessExcels);
        }
    }
}
